package com.xinruke.hostels.reservation.user.controller;

import com.xinruke.hostels.common.controller.BaseController;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.reservation.user.interfaces.dto.*;
import com.xinruke.hostels.reservation.user.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @ClassName LoginController
 * @CreateDate 2019/6/12
 * @Author FengXinQiang
 * @Version V1.0.0.0
 * @Decription 登录
 */
@RestController
@RequestMapping("login")
@Api(tags = "用户登录")
public class LoginController extends BaseController {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    LoginService loginService;

    @PostMapping("userLogin")
    @ApiOperation("登录")
    public ResultVO<LoginResultDTO> userLogin(@RequestBody @Valid LoginDTO loginDTO) {
        ResultVO resultVO = ResultVO.fail();

        try {
            resultVO = loginService.userLogin(loginDTO);
        } catch (Exception e) {
            logger.error("系统异常", e);
        }

        return resultVO;
    }

    @PostMapping("loginValidate")
    @ApiOperation("登录验证")
    public ResultVO<LoginReservationValidateResultDTO> loginValidate(@RequestBody @Valid LoginReservationValidateDTO loginValidateDTO) {
        ResultVO resultVO = ResultVO.fail();

        try {
            resultVO = loginService.loginValidate(loginValidateDTO);
        } catch (Exception e) {
            logger.error("系统异常", e);
        }

        return resultVO;
    }

    @PostMapping(value = "loginUserInfo")
    @ApiOperation(value = "查询登录用户信息")
    public ResultVO<LoginUserInfoQueryResultDTO> getLoginUserInfo(@RequestBody @Valid LoginUserInfoQueryDTO loginUserInfoQueryDTO) {
        ResultVO<LoginUserInfoQueryResultDTO> resultVO = ResultVO.fail();

        try {
            resultVO = loginService.getLoginUserInfo(loginUserInfoQueryDTO);
        } catch (Exception e) {
            logger.error("系统异常", e);
        }

        return resultVO;
    }

    @PostMapping("userLoginOut")
    @ApiOperation("退出")
    public ResultVO userLoginOut() {
        ResultVO resultVO = ResultVO.fail();

        try {
            resultVO = loginService.userLoginOut();
        } catch (Exception e) {
            logger.error("系统异常", e);
        }

        return resultVO;
    }

}
